#include "mainwindow_game1.h"
#include "ui_mainwindow_game1.h"
#include <QDebug>
MainWindow_game1::MainWindow_game1(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow_game1)
{
    game1_scene();
    player_pic.load(player_picture_1);
    label = new QLabel(this);
    label->setPixmap(player_pic);
    label->resize(80,80);
    label->setScaledContents(true);
    label->move(player_x,player_y);
//    qDebug()<<"lalala";


    QTimer *_time=new QTimer();
   _time->start(5);
    connect(_time,SIGNAL(timeout()),this,SLOT(player_move()));

}

MainWindow_game1::~MainWindow_game1()
{
    delete ui;
}

void MainWindow_game1::keyPressEvent(QKeyEvent *e)
{
    if(e->key()==Qt::Key_Left)
    {
        flag[2]=1;
        direction=3;
    }
    if(e->key()==Qt::Key_Right)
    {
        flag[3]=1;
        direction=1;
     }
    if(e->key()==Qt::Key_Up)
    {
       flag[0]=1;
       direction=0;
    }
    if(e->key()==Qt::Key_Down)
    {
        flag[1]=1;
        direction=2;
    }

    update();
}

void MainWindow_game1::keyReleaseEvent(QKeyEvent *e)
{
    if(e->key()==Qt::Key_Up) flag[0]=0;
    if(e->key()==Qt::Key_Down) flag[1]=0;
    if(e->key()==Qt::Key_Left) flag[2]=0;
    if(e->key()==Qt::Key_Right) flag[3]=0;
    update();
//        qDebug()<<"lalala"<<flag[0];
}



void MainWindow_game1::game1_scene()
{
    //设置窗口尺寸（固定）
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    //标题
    setWindowTitle(GAME1_TITLE);
    //窗口图标
    setWindowIcon(QIcon(GAME_Icon));
//        qDebug()<<"lalala";
}



void MainWindow_game1::UPDATE()
{
    update();
    //    qDebug()<<"lalala";
}

void MainWindow_game1::player_move()
{
    if(flag[3]==1){
        if(player_x<880)player_x++;
        if(action%60<20){
            player_pic.load(player_picture_7);
        }else if(action%60<40){
            player_pic.load(player_picture_8);
        }else{
            player_pic.load(player_picture_9);
        }
    }
    if(flag[2]==1){
        if(player_x>0)player_x--;
        if(action%60<20){
            player_pic.load(player_picture_4);
        }else if(action%60<40){
            player_pic.load(player_picture_5);
        }else{
            player_pic.load(player_picture_6);
        }
    }
    if(flag[0]==1){
        if(player_y>0)player_y--;
        if(action%60<20){
            player_pic.load(player_picture_10);
        }else if(action%60<0){
            player_pic.load(player_picture_11);
        }else{
            player_pic.load(player_picture_12);
        }
    }
    if(flag[1]==1){
        if(player_y<560)player_y++;
        if(action%60<20){
            player_pic.load(player_picture_1);
        }else if(action%60<40){
            player_pic.load(player_picture_2);
        }else{
            player_pic.load(player_picture_3);
        }
    }
    if(!(flag[1]+flag[2]+flag[3]+flag[0])){
        if(direction==0) player_pic.load(player_picture_11);
        if(direction==1) player_pic.load(player_picture_8);
        if(direction==2) player_pic.load(player_picture_2);
        if(direction==3) player_pic.load(player_picture_5);
    }

    action++;
    label->setPixmap(player_pic);
    label->move(player_x,player_y);
//    qDebug()<<"lalala"<<player_x;
    update();

}
void MainWindow_game1::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap(m_map2.m_map1_posx,m_map2.m_map1_posy,m_map2.m_map1_2);//  尝试绘制地图场景
    //绘制角色
//    painter.drawPixmap(player_x,player_y,player_pic);

}
